const { createConnection } = require('../config/dbConfig');
const favoriteDBConfig = require('../config/favoriteDBConfig');

const getFavoritesByUserId = async (userId) => {
    const connection = await createConnection(favoriteDBConfig);
    const [rows] = await connection.execute('SELECT game_id FROM favorites WHERE user_id = ?', [userId]);
    connection.end();
    return rows.map(row => row.game_id);
};

const addFavorite = async (userId, gameId) => {
    const connection = await createConnection(favoriteDBConfig);
    await connection.execute('INSERT INTO favorites (user_id, game_id) VALUES (?,?)', [userId, gameId]);
    connection.end();
};

const removeFavorite = async (userId, gameId) => {
    const connection = await createConnection(favoriteDBConfig);
    await connection.execute('DELETE FROM favorites WHERE user_id = ? AND game_id = ?', [userId, gameId]);
    connection.end();
};

module.exports = {
    getFavoritesByUserId,
    addFavorite,
    removeFavorite
};